#pragma once

#include <iostream>
#include "InetAddr.hpp"
#include "Socket.hpp"
using namespace SocketModule;

class Lister
{
public:
  Lister(int port) : _listensoke(std::make_unique<TcpSocket>()), // build listen socket
                     _pot(port)
  {
    _listensoke->BuildTcpSockeMethod(_pot);
  }
  void Accepter() // accepter
  {
    // _listensoke->Accepter();
    // LOG(LogLevel::DEBUG) << "Accepter hhhhhhhhhhhhhhhhhh";
    while (true)
    {
      InetAddr addr;
      int error;
      int connfd = _listensoke->Accepter(&addr, &error);
      if (connfd > 0)
      {
        // 获取连接成功
      }
      else
      {
        // 获取连接失败
        if (error == EAGAIN || error == EWOULDBLOCK)
        {
          LOG(LogLevel::DEBUG) << "Accepter EAGAIN";
          break;
        }
        else if (error = EINTR)
        {
          LOG(LogLevel::DEBUG) << "Accepter EWOULDBLOCK";
          break;
        }
        else
        {
          LOG(LogLevel::ERROR) << "Accepter error";
          break;
        }
      }
    }
  }
  int Sockfd() // get socket fd
  {
    return _listensoke->fd();
  }

  ~Lister()
  {
    _listensoke->close();
  }

private:
  int _pot;                            // port
  std::unique_ptr<Socket> _listensoke; // listen socket
};